'use strict'

var

    form = $('#test-form'),

    langs = form.find('[name=lang]'),

    selectAll = form.find('label.selectAll :checkbox'),

    selectAllLabel = form.find('label.selectAll span.selectAll'),

    deselectAllLabel = form.find('label.selectAll span.deselectAll'),

    invertSelect = form.find('a.invertSelect');


langs.click(function(){

    if(langs.not(':checked').length===0){

        selectAll.prop('checked',true);

    }

});

// langs.click(function(){
//     if(langs.not(':checked').length===0){
//         selectAll.prop('checked',true);
//     }
// });


selectAll.click(function(){

    if( selectAll.is(':checked') ){

        langs.prop('checked',true);

        selectAllLabel.css('display','none');

        deselectAllLabel.css('display','inline');

    }

    else{

        langs.prop('checked',false);

        selectAllLabel.css('display','inline');

        deselectAllLabel.css('display','none');

    }

});

invertSelect.click(function(){

    langs.map(function(){

        var ch=$(this);

        if(ch.prop('checked')){

            ch.prop('checked',false);

        }

        else{

            ch.prop('checked',true);

        }

        if(selectAll.prop('checked')){

            selectAll.prop('checked',false);

        }

        else{

            selectAll.prop('checked',true);

        }

    });

})